<template>
  <div>
    <div class="app_card mb-2 pt-4 pb-4">
      <div class="d-flex justify-content-end align-items-center">
        <el-button type="primary" @click="onAdd()">添加</el-button>
      </div>

    </div>
    <div class="app_card pt-4  pb-5">
      <el-table stripe size="large" :data="dataOfTable" height="calc(100vh - 300px)" sortable="custom" @sort-change="handleSortChange" @selection-change="handleSelectionChange">
        <el-table-column label="任务名称" prop="taskName" align="center">
          <template #default="scope">{{ scope.row.taskName }}</template>
        </el-table-column>
        <el-table-column label="任务描述" prop="taskDescription" align="center">
          <template #default="scope">{{ scope.row.taskDescription }}</template>
        </el-table-column>
        <el-table-column label="任务时间" prop="taskModel" align="center" show-overflow-tooltip>
          <template #default="scope">{{ scope.row.taskModel=='day'?'每天':'每周' }}{{scope.row.dayHour}}点执行</template>
        </el-table-column>

        <el-table-column label="教室数量" prop="tackRoom" align="center" show-overflow-tooltip>
          <template #default="scope">{{scope.row.tackRoom}}</template>
        </el-table-column>
        <el-table-column label="任务状态" prop="status" align="center" show-overflow-tooltip>
          <template #default="scope">
            <el-switch size="large" v-model="scope.row.status" inline-prompt active-value="1" inactive-value="2" active-text="启用" inactive-text="停用" />
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="120">
          <template #default="scope">
            <el-button link type="primary" @click="onEditRow(scope.row)">编辑</el-button>
            <el-button link type="primary" @click="onTasklogRow(scope.row)">日志</el-button>
            <!-- <el-button link type="danger" @click="onDeleteRow(scope.row)">删除</el-button> -->
          </template>
        </el-table-column>

      </el-table>
      <div class="mt-2">
        <el-pagination background v-model:currentPage="queryParam.page" v-model:page-size="queryParam.pageSize" :page-sizes="paginationOption.pageSizes" :background="false" :layout="paginationOption.layout" :total="paginationOption.total" @size-change="handleChangeSize" @current-change="handleChangeCurrent" />
      </div>
    </div>

    <div>
    </div>
    <EditDialog ref="editDialogRef" @success="loadData()"></EditDialog>
    <TasklogDialog ref="tasklogDialogRef" @success="loadData()"></TasklogDialog>
  </div>
</template>

<script setup>
/**
 * 教室管理
 */
import { ref, onMounted } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import EsAjax from '@/utils/EsAjax';
import ApiUrls from '@/apis/index';
import useAuthStore from '@/stores/useAuthStore';

//组件
import EditDialog from './EditDialog.vue';
import TasklogDialog from './TasklogDialog.vue';

// ======================================================
//                      变量声明
// ======================================================





const authStore = useAuthStore();

// 组件引用
const editDialogRef = ref(null);
const tasklogDialogRef = ref(null);

// 表格数据
const dataOfTable = ref([]);
// 表格查询参数
const queryParam = ref({
  q: '',
  search: '',
  orderby: '',
  group_id: 0,
  page: 1,
  pageSize: 10,
})
// 分页栏配置数据
const paginationOption = ref({
  layout: 'total, sizes, prev, pager, next, jumper',
  pageSizes: [10, 20, 50, 100, 500, 1000],
  total: 0,
})
// 教室分组数据
const dataOfGroups = ref([]);
const defaultProps = {
  children: 'children',
  label: 'label',
}
//1大图 2小图
const radioFloor = ref('-1');
const radioIcon = ref('1');
const col12 = ref(12);
const col8 = ref(8);








// ======================================================
//                      点击操作
// ======================================================




// 点击刷新按钮
const onRefresh = () => {
  queryParam.value.page = 1;
  loadData();
}

// 点击新增按钮
const onAdd = (row = null) => {
  if (row) {
    editDialogRef.value.show(0, "添加" + row.title, row);
    return;
  }
  editDialogRef.value.show(0, "添加任务管理");
}


// 点击批量删除按钮
const onDeleteMulti = () => {
  let n = multipleSelection.value.length, id = multipleSelection.value.map(v => v.id).toString();
  ElMessageBox.confirm("确定要删除 " + n + " 条记录吗？", "警告").then(() => {
    EsAjax.delete(ApiUrls.articleCategoryDelete, { data: { id } }).then(res => {
      if (res.data.code !== 200) {
        ElMessage.warning(res.data.msg);
        return;
      }
      ElMessage.success("操作成功");
      loadData();
    }).catch(err => {
      ElMessage.error("网络错误：" + err.message);
    })
  }).catch(() => { })
}



// 点击行内编辑按钮
const onEditRow = (row) => {
  editDialogRef.value.show(row.id, "编辑任务" + row.taskName,row);
}
// 点击行内编辑按钮
const onTasklogRow = (row) => {
  tasklogDialogRef.value.show(row.id, row.taskName + "的日志");
}

// 点击行内删除按钮
const onDeleteRow = (row) => {
  ElMessageBox.confirm("确定要删除该记录吗？", "警告").then(() => {
    EsAjax.delete(ApiUrls.articleCategoryDelete, { data: { id: row.id } })
      .then(res => {
        if (res.data.code !== 200) {
          ElMessage.warning(res.data.msg);
          return;
        }
        loadData();
      })
      .catch(err => { ElMessage.error("网络错误：", err.message) });
  }).catch(() => { });
  console.log(row)
}


// 处理分页尺寸
const handleChangeSize = (val) => {
  console.log(`${val} items per page`);
  queryParam.value.page = 1;
  queryParam.value.pageSize = val;
  loadData();
}

// 处理页码
const handleChangeCurrent = (val) => {
  console.log(`current page: ${val}`)
  queryParam.value.page = val;
  loadData();
}

// ======================================================
//                      处理事件
// ======================================================

const loadData = () => {
  EsAjax.post(ApiUrls.taskList, queryParam.value).then(res => {
    if (res.data.code !== 200) {
      ElMessage.warning(res.data.msg);
      return;
    }
    console.log(res.data.data)
    dataOfTable.value = res.data.data.data;
    paginationOption.value.total = res.data.data.total;
  }).catch(err => {
    ElMessage.error(err.message);
  })
}


onMounted(() => {
  loadData();
})

</script>
<style lang="less" scoped>
.class_name {
  position: absolute;
  font-size: 20px;
  left: 30px;
  top: 10px;
  font-weight: bold;
}
</style>

